package com.qifenqian.turbot.module.ui.restaurant

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.module.adapter.restaurant.RestaurantServiceAdapter
import com.qifenqian.turbot.module.contract.RestaurantServiceContract
import com.qifenqian.turbot.module.entity.request.ServiceRequest
import com.qifenqian.turbot.module.entity.response.restaurant.RestaurantServiceEntity
import com.qifenqian.turbot.module.presenter.RestaurantServicePresenter
import com.qifenqian.turbot.util.Lcat
import com.qifenqian.turbot.util.SpUtils
import kotlinx.android.synthetic.main.activity_restaurant_service.*
import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2019/1/7 15:13
 * @description 餐厅服务
 * @see
 * @since
 */
class RestaurantServiceActivity : BaseActivity<RestaurantServicePresenter, RestaurantServiceContract.View>(), RestaurantServiceContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, RestaurantServiceActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_restaurant_service
    }

    override fun bindPresenter(): RestaurantServicePresenter? {
        return RestaurantServicePresenter(this, this)
    }

    private var mDeletePosition = -1
    override fun initial() {
        mPresenter!!.getServiceList()
        rv_restaurant_service.layoutManager = GridLayoutManager(this, 3)
        mRestaurantServiceAdapter = RestaurantServiceAdapter(mRestaurantServiceEntities)
        rv_restaurant_service.adapter = mRestaurantServiceAdapter
        btn_restaurant_service_add.setOnClickListener {
            showEditDialog()
        }
        mRestaurantServiceAdapter!!.setOnChildClickListener(object : RestaurantServiceAdapter.OnItemChildClickListener {
            override fun onDelete(position: Int, serviceId: Int) {
                mPresenter!!.deleteService(serviceId)
                mDeletePosition = position
            }
        })
        btn_restaurant_service_next.setOnClickListener {
            val serviceIds: MutableList<ServiceRequest.ServiceId> = mutableListOf()
            for (restaurantServiceEntity in mRestaurantServiceEntities) {
                if (restaurantServiceEntity.isChoose == 1) {
                    serviceIds.add(ServiceRequest.ServiceId(restaurantServiceEntity.id))
                }
            }
            val request = ServiceRequest(SpUtils.getInstance().getInt(Constant.SP_KEY_RESTAURANT_ID, -1, Constant.SP_ACCOUNT), serviceIds)
            // TODO: 2019/1/7 接口问题,上传之后,isChoose依然没有改变 ----->
            mPresenter!!.chooseService(request)
        }
    }

    private var mRestaurantServiceEntities = mutableListOf<RestaurantServiceEntity>()

    private var mRestaurantServiceAdapter: RestaurantServiceAdapter? = null
    private var mEditDialog: Dialog? = null
    private fun showEditDialog() {
        mEditDialog = Dialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null)
        mEditDialog!!.setContentView(view)
        mEditDialog!!.show()
        view.tv_dialog_edit_title.text = "添加服务"
        view.btn_dialog_edit_cancel.setOnClickListener { mEditDialog!!.dismiss() }
        view.btn_dialog_edit_ok.setOnClickListener {
            if (view.et_dialog_edit_content.text.toString().isEmpty()) {
                toast("请输入服务内容")
                return@setOnClickListener
            }
            mPresenter!!.addService(view.et_dialog_edit_content.text.toString().trim())
        }
    }

    override fun getServiceListResult(isSuccess: Boolean, code: Int, msg: String?, restaurantServiceEntities: List<RestaurantServiceEntity>?) {
        if (isSuccess) {
            mRestaurantServiceEntities.clear()
            mRestaurantServiceEntities.addAll(restaurantServiceEntities as MutableList<RestaurantServiceEntity>)
            mRestaurantServiceAdapter!!.notifyDataSetChanged()
            Lcat.a("mRestaurantServiceEntities", mRestaurantServiceEntities)
        } else {
            toast(msg!!)
            finish()
        }
    }

    override fun addServiceResult(isSuccess: Boolean, code: Int, msg: String?, restaurantServiceEntity: RestaurantServiceEntity?, serviceName: String) {
        if (isSuccess) {
            mRestaurantServiceEntities.add(restaurantServiceEntity!!)
            mRestaurantServiceAdapter!!.notifyDataSetChanged()
            mEditDialog!!.dismiss()
        }
        toast(msg!!)
        Lcat.a("mRestaurantServiceEntities", mRestaurantServiceEntities)
    }

    override fun deleteServiceResult(isSuccess: Boolean, code: Int, msg: String?) {
        Lcat.a("mDeletePosition", mDeletePosition)
        if (isSuccess) {
            mRestaurantServiceEntities.removeAt(mDeletePosition)
            mRestaurantServiceAdapter!!.notifyItemRemoved(mDeletePosition)
        }
        toast(msg!!)
        Lcat.a("mRestaurantServiceEntities", mRestaurantServiceEntities)
    }

    override fun chooseServiceResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            finish()
        }
        toast(msg!!)
        Lcat.a("mRestaurantServiceEntities", mRestaurantServiceEntities)
    }
}
